import base64
import hashlib
import random
import string


class UserService():

    @staticmethod
    def genePwd(pwd, salt):
        m = hashlib.md5()
        String = "%s-%s" % (base64.encodebytes(pwd.encode("utf-8")), salt)
        m.update(String.encode('utf-8'))
        return m.hexdigest()

    @staticmethod
    def geneSalt(length=10):
        key_list = [random.choice((string.ascii_letters + string.digits)) for i in range(length)]
        return ''.join(key_list)

    @staticmethod
    def geneAuthCode(user_info=None):
        """
        cookie 校验字段
        :param user_info:
        :return:
        """
        m = hashlib.md5()
        str = "%s-%s-%s-%s-%s" % (user_info.id, user_info.login_name,
                                  user_info.login_pwd, user_info.login_salt, user_info.status)
        m.update(str.encode("utf-8"))
        return m.hexdigest()
